// Lee 20140513
#include<iostream>
#include<string.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>

#include"getip.h"
using namespace std;
char * GetIpByHost(const char * host)
{
	if(!host)
	{
		return NULL;
	}

	unsigned long inaddr = 0;
	char * result = NULL;
	int len = 0 ;
	inaddr = (unsigned long)inet_addr(host);
	if(inaddr!=INADDR_NONE)
	{
		len = strlen(host);
		result = new char[len+1];
		memset(result,0,len+1);
		memcpy(result,host,len);
		return result;

	}

	struct hostent *hp;
	hp = gethostbyname(host);//????
	if(hp==NULL){
		return NULL;
	}

	struct in_addr in;

	bcopy(*(hp->h_addr_list),(caddr_t)&in,hp->h_length);
	
	char abuf[INET_ADDRSTRLEN];
	if(inet_ntop(AF_INET,(void*)&in,abuf,sizeof(abuf))==NULL){
		cout<<"inet_btop() return error"<<endl;
		return NULL;
	}

	len = strlen(abuf);
	result = new char[len+1];
	memset(result,0,len+1);
	memcpy(result,abuf,len);
	
	return result;
}






